package com.pqtest.api.service;

import com.pqtest.api.domain.CollectionDomain;
import com.pqtest.api.dto.CollectionDto;
import com.pqtest.api.entity.CollectionEntity;
import com.pqtest.api.entity.PqCsTestItemEntity;
import com.pqtest.api.mapper.CollectionMapper;
import com.pqtest.api.mapper.PqCsTestItemMapper;
import com.pqtest.api.utils.DtoEntityTransformer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CollectionService {
    @Autowired
    CollectionMapper collectionMapper;
    @Autowired
    PqCsTestItemMapper pqCsTestItemMapper;
    @Autowired
    CollectionDomain collectionDomain;
    public int delById(int id,int userId) throws Exception {
        CollectionEntity collectionEntity = collectionMapper.getById(id);
        if(collectionEntity.getUserId() != userId){
            throw new Exception("no permission");
        }
        return collectionMapper.del(id);
    }

    @Cacheable(value = "collect",key="T(String).valueOf(#collectionDto.userId).concat('_').concat(#collectionDto.itemId)",unless = "#result == null")
    public int collect(CollectionDto collectionDto) throws Exception {
        CollectionEntity collectionEntity = collectionDomain.collect(collectionDto);
        PqCsTestItemEntity pqCsTestItemEntity = pqCsTestItemMapper.getById(collectionDto.getItemId());
        if(pqCsTestItemEntity.equals(null)){
            throw new Exception("No item data");
        }
        return collectionMapper.collect(collectionEntity);
    }

    @Transactional
    @CacheEvict(value = "collect",key="T(String).valueOf(#collectionDto.userId).concat('_').concat(#collectionDto.itemId)")
    public int deCollect(CollectionDto collectionDto){
        CollectionEntity collectionEntity = new CollectionEntity();
        DtoEntityTransformer.transalte(collectionDto,collectionEntity);
        return collectionMapper.deCollect(collectionEntity);
    }

    @Cacheable(value = "collect",key="T(String).valueOf(#collectionDto.userId).concat('_').concat(#collectionDto.itemId)",unless = "#result == null")
    public int wrong(CollectionDto collectionDto) throws Exception {
        CollectionEntity collectionEntity = collectionDomain.collect(collectionDto);
        PqCsTestItemEntity pqCsTestItemEntity = pqCsTestItemMapper.getById(collectionDto.getItemId());
        if(pqCsTestItemEntity.equals(null)){
            throw new Exception("No item data");
        }
        return collectionMapper.wrong(collectionEntity);
    }

    @Transactional
    @CacheEvict(value = "collect",key="T(String).valueOf(#collectionDto.userId).concat('_').concat(#collectionDto.itemId)")
    public int deWrong(CollectionDto collectionDto){
        CollectionEntity collectionEntity = new CollectionEntity();
        DtoEntityTransformer.transalte(collectionDto,collectionEntity);
        return collectionMapper.deWrong(collectionEntity);
    }
}
